package ch.supsi.parentalcontrol;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import ch.supsi.mobile.core.applications.AddressesApplication;
import ch.supsi.mobile.core.listeners.DoubleClick;

public class UserPanel extends JPanel{
	
	// dichiaro gli elementi che utilizzo per realizzare la grafica
	private GridBagConstraints containerLayout = new GridBagConstraints();
	private GridBagConstraints targetContainerLayout = new GridBagConstraints();
	
	// elementi scheda
	private JButton addObjectOk, testTarget;
	private JLabel userLabel, genericLabel, resultMessage;
	private JTextField newObject;
	private JList userList;
	private JScrollPane userScroll;
	private DefaultListModel userListModel;
	private DefaultListModel whiteListModel;
	private DefaultListModel blackListModel;
	private JTextField targetText;
	private JPanel targetPanel;
	
	private String type;
	
	public UserPanel(DefaultListModel cmodel, DefaultListModel wmodel, DefaultListModel bmodel, String type){
		this.type = type;
		initComponent(cmodel, wmodel, bmodel);
	}
	
	private void initComponent(DefaultListModel cmodel, DefaultListModel wmodel, DefaultListModel bmodel){
        addObjectOk = new JButton();
        userLabel = new JLabel();
        newObject = new JTextField();
        userList = new JList();
        userScroll = new JScrollPane();
        targetText = new JTextField(10);
        genericLabel = new JLabel();
        resultMessage = new JLabel("Result: ");
        targetPanel = new JPanel();
        testTarget = new JButton();
        
        // setto il layout al pannello del this
        GridBagLayout thisLayout = new GridBagLayout();
        this.setLayout(thisLayout);
        

        if(this.type.equals("address"))
        	userLabel.setText("Contatti:");
        else if(this.type.equals("browser"))
        	userLabel.setText("Web page:");
        containerLayout.gridx = 1;
        containerLayout.gridy = 0;
        this.add(userLabel, containerLayout);

        // definisco la white/black list
        whiteListModel = wmodel;
        blackListModel = bmodel;
        userListModel = cmodel;
        
        userList.setModel(userListModel);
        userList.addMouseListener(new DoubleClick(this));
        userScroll.setViewportView(userList);
        userScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        userScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        userScroll.setPreferredSize(new Dimension(200,300));
        containerLayout.gridx = 1;
        containerLayout.gridy = 1;
        this.add(userScroll, containerLayout);
        
        // definisce il messaggio corretto
        if(this.type.equals("address"))
        	genericLabel.setText("Inserisci il numero:");
        else if(this.type.equals("browser"))
        	genericLabel.setText("Inserisci l'URL:");
        

        if(this.type.equals("address"))
            testTarget.setText("Call");
        else if(this.type.equals("browser"))
            testTarget.setText("Go");
        testTarget.addActionListener(new GestioneSchedaUser(this));
        
        GridBagLayout targetLayout = new GridBagLayout();
        targetPanel.setLayout(targetLayout);
        targetPanel.setPreferredSize(new Dimension(300, 200));
        
        targetContainerLayout.gridx = 2;
        targetContainerLayout.gridy = 0;
        targetPanel.add(genericLabel, targetContainerLayout);
        
        targetContainerLayout.gridx = 2;
        targetContainerLayout.gridy = 1;
        targetPanel.add(targetText, targetContainerLayout);

        targetContainerLayout.gridx = 3;
        targetContainerLayout.gridy = 1;
        targetPanel.add(testTarget, targetContainerLayout);

        targetContainerLayout.gridx = 2;
        targetContainerLayout.gridy = 3;
        targetPanel.add(resultMessage, targetContainerLayout);
        

        containerLayout.gridx = 4;
        containerLayout.gridy = 1;
        this.add(targetPanel, containerLayout);
	}

	public String getType() {
		return type;
	}

	public JList getUserList() {
		return userList;
	}

	public DefaultListModel getUserListModel() {
		return userListModel;
	}

	public DefaultListModel getWhiteListModel() {
		return whiteListModel;
	}

	public DefaultListModel getBlackListModel() {
		return blackListModel;
	}
	
	public JTextField getTargetText(){
		return targetText;
	}
	
	public JLabel getResultMessage(){
		return resultMessage;
	}

}
